package com.mhtvs;

import java.util.HashMap;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.mhtvs.R;
import com.mhtvs.Tunnel;

//---------------------------------------------------------------
//Development source for MHTVS CLIENT
//"Android client for use with MHTVS"
//Copyright (c) 2011, Doug Thompson, perladdict@gmail.com
//All rights reserved.
//
//http://code.google.com/p/mhtvs/
//
//View Demo here:
//http://www.youtube.com/watch?v=v4M2uVrMrGk
//
//Redistribution and use in source and binary forms, with or
//without modification, are permitted provided that the following
//conditions are met:
//
//* Redistributions of source code must retain the above
//  copyright notice, this list of conditions and the
//  following disclaimer.
//* Redistributions in binary form must reproduce the
//  above copyright notice, this list of conditions and
//  the following disclaimer in the documentation and/or
//  other materials provided with the distribution.
//* Neither the name of the developer nor the names
//  of its contributors may be used to endorse or promote
//  products derived from this software without specific
//  prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
//SUCH DAMAGE.
//---------------------------------------------------------------


public class MainView extends MhtvsActivity {	
	protected static final String Status = null;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
        	setContentView(R.layout.main);
            TextView mytexttoset = (TextView) this.findViewById(R.id.text);
            mytexttoset.setText("Tunnels connected");
            WebView mWebView;
            mWebView = (WebView) findViewById(R.id.webView1);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setPluginsEnabled(true);
            mWebView.setBackgroundColor(Color.parseColor("#000000"));
				HashMap<String, String> sqliteConfsHash;
				try {
					sqliteConfsHash = TunnelInfo.getAllDetailsInHASH();
					String portforwardsLOCAL1 =  String.valueOf(sqliteConfsHash.get("portforwardsLOCAL0"));					
  					String serverPage1 =         String.valueOf(sqliteConfsHash.get("serverPage0"));
  					Toast.makeText(getApplicationContext(),"http://127.0.0.1:" + portforwardsLOCAL1 + "/" + serverPage1 , Toast.LENGTH_LONG).show();
  					mWebView.loadUrl("http://127.0.0.1:" + portforwardsLOCAL1 + "/" + serverPage1);  
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	return true;
        }
        else if(keyCode == KeyEvent.KEYCODE_HOME){
        	System.exit(0);
        }
        return super.onKeyDown(keyCode, event);
    }
    
    private WebView mWebView;

    public void onConfigurationChanged(Configuration newConfig){
    	super.onConfigurationChanged(newConfig);
    	
    	   if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO){
    		   super.onConfigurationChanged(newConfig);
    	   }
    	   if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES){
    		   super.onConfigurationChanged(newConfig);
    	   }	
    }

    protected void onCreate(Bundle savedInstanceState) {    	
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
               		
				Tunnel t=new Tunnel();
	  				try{ 		  				
	  					t.go(); 
	  					TextView mytexttoset = (TextView) this.findViewById(R.id.text);
	  					mytexttoset.setText("Tunnels connected");
	  					mWebView = (WebView) findViewById(R.id.webView1);
	  					mWebView.getSettings().setJavaScriptEnabled(true);
	  					mWebView.getSettings().setPluginsEnabled(true);
	  					mWebView.setBackgroundColor(Color.parseColor("#000000"));
	  					mWebView.clearCache(true);
	  					mWebView.clearHistory();
	  					HashMap<String,String> sqliteConfsHash = TunnelInfo.getAllDetailsInHASH();
	  					String portforwardsLOCAL1 = String.valueOf(sqliteConfsHash.get("portforwardsLOCAL0"));
	  					String serverPage1 =         String.valueOf(sqliteConfsHash.get("serverPage0"));
	  					Toast.makeText(getApplicationContext(),"http://127.0.0.1:" + portforwardsLOCAL1 + "/" + serverPage1 , Toast.LENGTH_LONG).show();
	  					mWebView.loadUrl("http://127.0.0.1:" + portforwardsLOCAL1 + "/" + serverPage1);        
	  				} catch(Exception ex) { 
	  					ex.printStackTrace(); 
	  					mWebView = (WebView) findViewById(R.id.webView1);
	  					mWebView.setBackgroundColor(Color.parseColor("#990000"));
	  					TextView mytexttoset = (TextView) this.findViewById(R.id.text);
	  					mytexttoset.setText("Tunnel connection failed");          
	  				}		
  			}

    @Override
    protected void onStart() {
        super.onStart();
        //Toast.makeText(this, "STARTED!", Toast.LENGTH_LONG).show();		
    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	//Toast.makeText(this, "RESTORED!", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onRestart() {
    	super.onRestart();
    	//Toast.makeText(this, "RESTARTED!", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //Toast.makeText(this, "RESUMED!", Toast.LENGTH_LONG).show();
    }    
    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState); // the UI component values are saved here.
        //Toast.makeText(this, "Activity state saved", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //Toast.makeText(this, "PAUSED!", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onStop() {
        super.onStop();
        //Toast.makeText(this, "STOPPED!", Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Toast.makeText(this, "Activity is getting killed", Toast.LENGTH_LONG).show();
    }
       
}